﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace AtletIS.Shared.Helpers
{
    public static class TextScoreBoardHelper
    {
        public static bool bConnected = false;

        #region Private Properties

        private static SerialPort comBoard;
        public static string startTime;

        private const byte stx = 2;
        private const byte etx = 3;

        private const byte lineOne = 111;
        private const byte lineTwo = 112;
        private const byte lineThree = 113;
        private const byte lineFour = 114;

        private const byte square_Sign = 94;

        private const byte let_t_Sign = 116;
        private const byte let_F_sign = 70;
        private const byte let_S_sign = 83;

        private const byte num_0_Sign = 48;
        private const byte num_1_sign = 49;
        private const byte num_2_sign = 50;
        private const byte num_3_sign = 51;
        private const byte num_4_sign = 52;
        private const byte num_5_sign = 53;
        private const byte num_6_sign = 54;
        private const byte num_7_sign = 55;
        private const byte num_8_sign = 56;
        private const byte num_9_sign = 57;

        #endregion

        /// <summary>
        ///  Inicializácia sériového portu
        /// </summary>
        public static void Init(string port)
        {
            comBoard = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
            if (comBoard != null)
                bConnected = true;
        }

        public static void DeInit()
        {
            if (comBoard != null)
            {
                comBoard.Close();
                comBoard.Dispose();
            }
        }

        /// <summary>
        ///  Zmaže vybraný riadok
        /// </summary>
        public static void ClearLine(byte adress)
        {
            byte[] boardData = new byte[5];
            boardData[0] = stx;
            boardData[1] = adress;
            boardData[2] = 66;
            boardData[3] = etx;

            byte chkSum = 0;
            for (int i = 0; i < boardData.Length - 1; i++)
            {
                chkSum ^= boardData[i];
            }

            boardData[4] = chkSum;

            if (!comBoard.IsOpen)
                comBoard.Open();

            if (comBoard.IsOpen)
                comBoard.Write(boardData, 0, boardData.Length);

            comBoard.Close();
        }

        /// <summary>
        ///  Zapíše prechod na tabuľu
        /// </summary>
        public static string WriteResult(int nStartNum, string sName, string sCheckPoint, TimeSpan tsPassingTime, byte adress)
        {
            try
            {
                // count time
                string timeToBoard = String.Format("{0:0}:{1:00}:{2:00}", tsPassingTime.Hours, tsPassingTime.Minutes, tsPassingTime.Seconds);

                byte[] arrStartNum = Encoding.ASCII.GetBytes(nStartNum.ToString());
                byte[] arrName = Encoding.GetEncoding("ibm852").GetBytes(sName);
                byte[] arrCheckPoint = Encoding.ASCII.GetBytes(sCheckPoint);
                byte[] arrTime = Encoding.ASCII.GetBytes(timeToBoard);

                int totalLength = 11 + arrStartNum.Length + 5 + arrName.Length + 5 + arrCheckPoint.Length + 5 + arrTime.Length + 2;
                int currentLength = 0;
                byte[] aTextForBoard = new byte[totalLength];

                aTextForBoard[0] = stx;
                aTextForBoard[1] = adress;
                aTextForBoard[2] = let_t_Sign;
                aTextForBoard[3] = num_0_Sign;
                aTextForBoard[4] = num_0_Sign;
                aTextForBoard[5] = num_0_Sign;
                aTextForBoard[6] = 51;
                aTextForBoard[7] = square_Sign;
                aTextForBoard[8] = let_F_sign;
                aTextForBoard[9] = num_2_sign;
                aTextForBoard[10] = square_Sign;
                currentLength = 11;

                // Add start number
                for (int i = currentLength; i < arrStartNum.Length + currentLength; i++)
                {
                    aTextForBoard[i] = arrStartNum[i - currentLength];
                }
                currentLength += arrStartNum.Length;

                // Set start of name
                aTextForBoard[currentLength] = square_Sign;
                aTextForBoard[currentLength + 1] = let_S_sign;
                aTextForBoard[currentLength + 2] = num_1_sign;
                aTextForBoard[currentLength + 3] = num_8_sign;
                aTextForBoard[currentLength + 4] = square_Sign;
                currentLength += 5;

                // Add name
                for (int i = currentLength; i < arrName.Length + currentLength; i++)
                {
                    aTextForBoard[i] = arrName[i - currentLength];
                }
                currentLength += arrName.Length;

                // Set start of check point
                aTextForBoard[currentLength] = square_Sign;
                aTextForBoard[currentLength + 1] = let_S_sign;
                aTextForBoard[currentLength + 2] = num_8_sign;
                aTextForBoard[currentLength + 3] = num_5_sign;
                aTextForBoard[currentLength + 4] = square_Sign;
                currentLength += 5;

                // Add checkPoint
                for (int i = currentLength; i < arrCheckPoint.Length + currentLength; i++)
                {
                    aTextForBoard[i] = arrCheckPoint[i - currentLength];
                }
                currentLength += arrCheckPoint.Length;

                // Set start position for time
                aTextForBoard[currentLength] = square_Sign;
                aTextForBoard[currentLength + 1] = let_S_sign;
                aTextForBoard[currentLength + 2] = num_9_sign;
                aTextForBoard[currentLength + 3] = num_9_sign;
                aTextForBoard[currentLength + 4] = square_Sign;
                currentLength += 5;

                // Add time
                for (int i = currentLength; i < arrTime.Length + currentLength; i++)
                {
                    aTextForBoard[i] = arrTime[i - currentLength];
                }
                currentLength += arrTime.Length;

                aTextForBoard[totalLength - 2] = etx;

                // Count check sum
                byte chkSum = 0;
                for (int i = 0; i < aTextForBoard.Length - 1; i++)
                {
                    chkSum ^= aTextForBoard[i];
                }

                aTextForBoard[totalLength - 1] = chkSum;

                if (!comBoard.IsOpen)
                    comBoard.Open();

                if (comBoard.IsOpen)
                    comBoard.Write(aTextForBoard, 0, aTextForBoard.Length);

                comBoard.Close();

                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        ///  Zapíše ľubovoľný text na tabuľu
        /// </summary>
        public static string WriteSimpleText(string sText, byte adress)
        {
            try
            {
                byte[] arrText = Encoding.GetEncoding("ibm852").GetBytes(sText);

                int totalLength = 11 + arrText.Length + 2;
                int currentLength = 0;
                byte[] aTextForBoard = new byte[totalLength];

                aTextForBoard[0] = stx;
                aTextForBoard[1] = adress;
                aTextForBoard[2] = let_t_Sign;
                aTextForBoard[3] = num_0_Sign;
                aTextForBoard[4] = num_0_Sign;
                aTextForBoard[5] = num_0_Sign;
                aTextForBoard[6] = 51;
                aTextForBoard[7] = square_Sign;
                aTextForBoard[8] = let_F_sign;
                aTextForBoard[9] = num_2_sign;
                aTextForBoard[10] = square_Sign;
                currentLength = 11;

                // Add Text
                for (int i = currentLength; i < arrText.Length + currentLength; i++)
                {
                    aTextForBoard[i] = arrText[i - currentLength];
                }
                currentLength += arrText.Length;

                aTextForBoard[totalLength - 2] = etx;

                // Count check sum
                byte chkSum = 0;
                for (int i = 0; i < aTextForBoard.Length - 1; i++)
                {
                    chkSum ^= aTextForBoard[i];
                }

                aTextForBoard[totalLength - 1] = chkSum;

                if (!comBoard.IsOpen)
                    comBoard.Open();

                if (comBoard.IsOpen)
                    comBoard.Write(aTextForBoard, 0, aTextForBoard.Length);

                comBoard.Close();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }




    }
}
